package com.tigerlee.candle.activity;

import java.util.Vector;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.tigerlee.candle.R;
import com.tigerlee.candle.spreadsheet.GoogleFormSender;
import com.tigerlee.candle.util.Constants;

public class CreateArticleActivity extends Activity implements OnClickListener {

	private EditText mTitleEdit;
	private EditText mNameEdit;
	private EditText mBodyEdit;

	private TextView mOkButton;
	private TextView mCancelButton;

	private static final int PROGRESS_DIALOG = 1;

	private static final int SUCCESS_POST = 0;
	private static final int FAIL_POST = 1;
	private static final int TIMEOUT_POST = 2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.create_article_custom_dialog_button);
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		setupView();
	}

	public void setupView() {
		// get all views in a layout
		mTitleEdit = (EditText) findViewById(R.id.title_edit_create_article);
		mNameEdit = (EditText) findViewById(R.id.name_edit_create_article);
		mBodyEdit = (EditText) findViewById(R.id.body_edit_create_article);
		mOkButton = (TextView) findViewById(R.id.ok_text_custom_button);
		mCancelButton = (TextView) findViewById(R.id.cancel_text_custom_button);
		
		// set & return color to default
		mOkButton.setBackgroundColor(Color.BLACK);
		mOkButton.setTextColor(Color.WHITE);
		mCancelButton.setBackgroundColor(Color.BLACK);
		mCancelButton.setTextColor(Color.WHITE);

		mOkButton.setOnClickListener(this);
		mCancelButton.setOnClickListener(this);
	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.ok_text_custom_button:
			if (mTitleEdit.getText().toString().equals("")
					|| mNameEdit.getText().toString().equals("")
					|| mBodyEdit.getText().toString().equals("")) {
				Toast.makeText(getApplicationContext(),
						getString(R.string.toast_fill_data), Toast.LENGTH_SHORT)
						.show();
				setupView();
				return;
			}
			new Thread(new Runnable() {
				public void run() {
					try {
						Vector<String> mValueVector = new Vector<String>();
						if (mTitleEdit.getText() != null) {
							mValueVector.add(mTitleEdit.getText().toString());
						}
						if (mNameEdit.getText() != null) {
							mValueVector.add(mNameEdit.getText().toString());
						}
						if (mBodyEdit.getText() != null) {
							mValueVector.add(mBodyEdit.getText().toString());
						}
						GoogleFormSender mFormSender = new GoogleFormSender(
								Constants.mArticleFormUrl);
						if (mFormSender.send(mValueVector) == true) {
							mHandler.removeMessages(TIMEOUT_POST);
							mHandler.sendMessage(mHandler
									.obtainMessage(SUCCESS_POST));
						}
					} catch (Exception e) {
						mHandler.removeMessages(TIMEOUT_POST);
						mHandler.sendMessage(mHandler.obtainMessage(FAIL_POST));
					}
				}
			}).start();
			break;
		case R.id.cancel_text_custom_button:
			finish();
			break;
		default:
			break;
		}
	}

	protected Dialog onCreateDialog(int id) {
		switch (id) {
		case PROGRESS_DIALOG:
			ProgressDialog dialog = new ProgressDialog(this);
			dialog.setMessage(getString(R.string.text_sending_dialog));
			dialog.setIndeterminate(true);
			dialog.setCancelable(false);
			return dialog;
		default:
			break;
		}
		return super.onCreateDialog(id);
	};

	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			removeDialog(PROGRESS_DIALOG);
			switch (msg.what) {
			case SUCCESS_POST:
				setResult(RESULT_OK);
				finish();
				break;
			case TIMEOUT_POST:
			case FAIL_POST:
				setResult(RESULT_CANCELED);
				Toast.makeText(getApplicationContext(),
						getString(R.string.toast_fail_no_connection),
						Toast.LENGTH_SHORT).show();
				finish();
				break;
			default:
				break;
			}
		}
	};

}
